## ocr_util.py
import os
import time

from paddleocr import PaddleOCR, draw_ocr





# Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换
# 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`
def text_ocr(img_path, language):
    startTime = int(round(time.time() * 1000))
    # print("start", datetime.datetime.now())

    ocr = PaddleOCR(use_angle_cls=True, lang=language)  # need to run only once to download and load model into memory
    print("------------------------加载用时------------------------：", int(round(time.time() * 1000)) - startTime, "毫秒")

    startTime = int(round(time.time() * 1000))
    result = ocr.ocr(img_path, cls=True)
    print("------------------------识别用时------------------------：", int(round(time.time() * 1000)) - startTime, "毫秒")

    final_txt = []
    for idx in range(len(result)):
        res = result[idx]
        for line in res:
            final_txt.append(line[1][0])
    print("\n".join(final_txt))
    # os.remove(img_path)  # 最后删除该文件
    return "".join(final_txt)

if __name__ == '__main__':
    text_ocr("C:/Users/Administrator/Desktop/raw.png","ch")